package org.seven.jrdp.biz.t.service;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.seven.jrdp.biz.t.mapper.TCrkMapper;
import org.seven.jrdp.biz.t.mapper.TFxdMapper;
import org.seven.jrdp.biz.t.mapper.TRkdMapper;
import org.seven.jrdp.biz.t.plugin.ys.YsPlugin;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class TCrkService {
	@Autowired
	private TFxdMapper tFxdMapper;
	@Autowired
	private TCrkMapper tCrkMapper;
	@Autowired
	private TRkdMapper tRkdMapper;
	@Autowired
	private TYsService tYsService;

	@Transactional(readOnly = true)
	public PageInfo<Map<String, Object>> find(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		List<Map<String, Object>> list = tCrkMapper.find(map);
		for (Map<String, Object> crk : list) {
			String cpbh = (String) crk.get("cpbh");
			List<String> kqmc = tRkdMapper.findKqmc(cpbh);
			Set<String> set = new LinkedHashSet<String>();
			for (String s : kqmc) {
				set.add(s);
			}
			crk.put("kqmc", set);
			crk.put("ckjl", tCrkMapper.findCkjl(cpbh));
			List<Map<String, Object>> fxmx = tFxdMapper.findMx(cpbh);
			for (Map<String, Object> ys : fxmx) {
				YsPlugin.check(ys);
			}
			crk.put("fxmx", fxmx);
		}
		return new PageInfo<Map<String, Object>>(list);
	}

	@Transactional(readOnly = true)
	public Map<String, Object> findDc(Map<String, Object> map) {
		Map<String, Object> dc = new HashMap<String, Object>();
		List<Map<String, Object>> list = tCrkMapper.find(map);
		for (Map<String, Object> crk : list) {
			String cpbh = (String) crk.get("cpbh");
			List<String> kqmc = tRkdMapper.findKqmc(cpbh);
			Set<String> set = new LinkedHashSet<String>();
			for (String s : kqmc) {
				set.add(s);
			}
			crk.put("kqmc", set);
			crk.put("ckjl", tCrkMapper.findCkjl(cpbh));
			List<Map<String, Object>> fxmx = tFxdMapper.findMx(cpbh);
			for (Map<String, Object> ys : fxmx) {
				YsPlugin.check(ys);
			}
			crk.put("fxmx", fxmx);
		}
		dc.put("list", list);
		dc.put("ys", tYsService.find());
		return dc;
	}
}